﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Entity.Validation;
using HYI.SDBSERVER;
using eHCBaseClass;
using Sunny.UI;

namespace SHRSTOCK
{
    public partial class FMHOLIDAY : SDBSERVER.FMSINGLE
    {
        //入口函数
        public static Int32 StartMe(string sMainPara, string sPara, string sUser, DBSERVER sDB)
        {
            StaticClass.Selectm_MainPara = sMainPara;
            StaticClass.SelectItemValue = sPara;
            StaticClass.SelectUser = sUser;
            StaticClass.myDB = sDB;
            return 0;
        }

        private int iGridView = 0;
        private string sType = "V";
        private string sSite_No = string.Empty;
        //DBSERVER myADO = new DBSERVER();
        DBCache myCache = new DBCache();

        public FMHOLIDAY()
        {
            InitializeComponent();
        }

        //查询代码
        protected override void OnSys04()
        {
            string SQL = " select case ftype when 1 then '节假日' else '工作日' end ftypenae,* "
             + " from a_ts_holiday"
             + " where 1=1";

            if (qiyear.Text != "")
            {
                SQL = SQL + " and year(fbegdate)=" + qiyear.Text ;
            }

            if (uiholidy.Text != "")
            {
                SQL = SQL + " and fholidayname like '%" + uiholidy.Text + "%'";
            }

            SQL = SQL + "  order by fbegdate desc";
            Qrytable = StaticClass.myDB.query(SQL);

            base.OnSys04();
            rowindex = -1;

        }

        private void FMHOLIDAY_Load(object sender, EventArgs e)
        {
            _INPUTTABLE = "import_i_ts_stock";
            sUser = StaticClass.SelectUser;
            string[] strArray = StaticClass.Selectm_MainPara.Split(new char[] { ';' });
            sMenu = strArray[1];
            sSite_No = strArray[0];

            //按钮控制
            sys05.Visible = false; sys05.Tag = "1";
            sys06.Visible = false; sys06.Tag = "1";
            sys07.Visible = false; sys07.Tag = "1";
            sys08.Visible = false; sys08.Tag = "1";
            sys09.Visible = false; sys09.Tag = "1";
            sys11.Visible = false; sys11.Tag = "1";

            qiftype.SelectedIndex = 0;

            //执行查询代码
            OnSys04();

            //表格列显示统一控制
            if (iGridView == 0)
            {
                myCache.doSetGridVIew(sMenu, sUser, uiDGView);
                iGridView = iGridView + 1;
            }
        }

        //新增代码
        protected override void OnSys01()
        {
            uifholidayname.Text = "";
            uifbegdate.Text = DateTime.Now.ToString("yyyy-MM-dd");
            uifenddate.Text = DateTime.Now.ToString("yyyy-MM-dd");
            DataTable myNo = StaticClass.myDB.query("exec fun_billnumber 2000");
            if ((myNo != null) && (myNo.Rows.Count > 0))
            {
                uifholidayid.Text = myNo.Rows[0][0].ToString();
            }
            cbfType.SelectedIndex = 0;
            sType = "A";
            doLoadData();
            uifholidayname.ReadOnly = false;

            base.OnSys01();
        }

        //编辑+浏览载入数据
        private void doLoadData()
        {
            //控制界面输入
            Boolean input = false;
            if ((sType.Equals("A")) || (sType.Equals("B")))
            {
                input = false;
            }
            else
            {
                input = true;
            }
            foreach (Control ctrl in uiPanelData.Controls)
            {
                if (ctrl is UITextBox)
                {
                    UITextBox m = (UITextBox)ctrl;
                    m.ReadOnly = input;
                }

                if (ctrl is UIComboBox)
                {
                    UIComboBox m = (UIComboBox)ctrl;
                    m.ReadOnly = input;
                }
            }

            if (!sType.Equals("A"))
            {
                uifholidayid.Text= uiDGView["fholidayid", rowindex].Value.ToString();
                uifholidayname.Text = uiDGView["fholidayname", rowindex].Value.ToString();
                string dt = uiDGView["fbegdate", rowindex].Value.ToString();
                DateTime mydt = Convert.ToDateTime(dt);
                uifbegdate.Text = mydt.ToString("yyyy-MM-dd");

                dt = uiDGView["fenddate", rowindex].Value.ToString();
                mydt = Convert.ToDateTime(dt);
                uifenddate.Text = mydt.ToString("yyyy-MM-dd");

                string iType= uiDGView["ftype", rowindex].Value.ToString();
                cbfType.SelectedIndex = int.Parse(iType) - 1;
            }

        }

        //编辑代码
        protected override void OnSys02()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }
            sType = "B";

            doLoadData();

            uifholidayname.ReadOnly = false;

            base.OnSys02();
        }

        //删除代码
        protected override void OnSys03()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }

            if (this.ShowAskDialog("确定删除(" + uiDGView["fholidayname", rowindex].Value.ToString() + ")?"))
            {
                string sBillNo = uiDGView["fholidayid", rowindex].Value.ToString();
                string SQL = "exec proc_biz_dodelete 'a_ts_holiday','','" + sBillNo + "',2,'" + sUser + "'";
                DataTable myTable = StaticClass.myDB.query(SQL);

                string sMsg = myTable.Rows[0][0].ToString();
                if (sMsg.Equals("OK"))
                {
                    this.ShowInfoDialog("删除完毕");
                    myCache.doCache();
                    OnSys04();
                }
                else
                {
                    this.ShowInfoDialog(sMsg);
                }
            }
        }

        //浏览代码
        protected override void OnSys10()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }
            sType = "V";

            doLoadData();

            base.OnSys10();

        }

        //数据为空检查
        private bool CheckData()
        {
            return CheckEmpty(uifholidayname, "请输入假期说明");
        }

        //保存代码
        protected override void OnSys08()
        {
            if (!CheckData())
            {
                return;
            }

            string dt1 = uifbegdate.Text;
            string dt2 = uifenddate.Text;
            if(Convert.ToDateTime(dt1)> Convert.ToDateTime(dt2))
            {
                this.ShowInfoDialog("开始日期不能大于结束日期");
                return; 
            }

            if (sType == "A")
            {
                try
                {
                    using (XTBasEntities context = new XTBasEntities())
                    {
                        a_ts_holiday myData = context.a_ts_holiday.Find(int.Parse(uifholidayid.Text));
                        if (myData is null)
                        {
                            a_ts_holiday myData2 = new a_ts_holiday();
                            myData2.fholidayname = uifholidayname.Text;
                            myData2.fholidayid =int.Parse(uifholidayid.Text);
                            string dt = uifbegdate.Text;
                            myData2.fbegdate = Convert.ToDateTime(dt);
                            dt = uifenddate.Text;
                            myData2.fenddate = Convert.ToDateTime(dt);
                            myData2.ftype = cbfType.SelectedIndex+1;
                            myData2.fsite_no = sSite_No;
                            myData2.foperator = sUser;
                            myData2.fupddate = DateTime.Now;
                            myData2.fsite_no = sSite_No;
                            context.a_ts_holiday.Add(myData2);
                            context.SaveChanges();
                            UIMessageTip.ShowOk("添加成功");

                        }
                        else
                        {
                            //Console.WriteLine("添加成功");
                            UIMessageTip.ShowOk("编号已存在");
                            //Console.WriteLine("已存在");
                            return;
                        }

                    }
                }
                catch (DbEntityValidationException dbEx)
                {
                    Console.WriteLine("错误" + dbEx.Message);
                }
            }

            else if (sType == "B")
            {
                using (XTBasEntities context = new XTBasEntities())
                {
                    //查询
                    a_ts_holiday myts_stock = context.a_ts_holiday.Find(int.Parse(uifholidayid.Text));
                    myts_stock.fholidayname = uifholidayname.Text;
                    string dt = uifbegdate.Text;
                    myts_stock.fbegdate = Convert.ToDateTime(dt);
                    dt = uifenddate.Text;
                    myts_stock.fenddate = Convert.ToDateTime(dt);
                    myts_stock.ftype = cbfType.SelectedIndex + 1;
                    myts_stock.foperator = sUser;
                    myts_stock.fupddate = DateTime.Now;
                    int result = context.SaveChanges();
                    UIMessageTip.ShowOk("修改成功");
                }
            }

            //刷新cache
            sType = "V";
            base.OnSys08();

            OnSys04();
        }


    }
}
